<?php
class Officialcources_model extends CRM_Model{
	private $typeDocRid;
	public function __construct(){
		parent::__construct();
		$typeDocRow = $this->GetTypeDoc();
		$this->typeDocRid = $typeDocRow->rid; 
	}
	
	public function GetDS(){
		$this->db->select('_documents.rid, 
							DATE_FORMAT(_officialcources_headers.date_doc, \'%d.%m.%Y\') as date_doc, 
							_officialcources_rows._currencies_rid as _currencies_rid,
							DATE_FORMAT(_officialcources_rows.cource_date, \'%d.%m.%Y\') as cource_date,
							_officialcources_rows.cource_value as cource_value,
							_currencies.code as currency_code,
							DATE_FORMAT(_documents.modifyDT, \'%d.%m.%Y %H:%i\') as modifyDT, 
							_documents.descr as descr, _documents.archive
							');
		$this->db->from('_documents');
		$this->db->join('_officialcources_headers', '_officialcources_headers._documents_rid = _documents.rid');
		$this->db->join('_officialcources_rows', '_officialcources_rows._officialcources_headers_rid = _officialcources_headers.rid');
		$this->db->join('_currencies', '_officialcources_rows._currencies_rid = _currencies.rid');
		$this->db->where(array('_documents._documents_types_rid'=>$this->typeDocRid));
		if($searchRule = $this->ciObject->GetSessionData('searchrule')){
			$whereArr = array();
			if(isset($searchRule['_officialcources_headers.date_doc>='])) { $whereArr['_officialcources_headers.date_doc>='] = $this->toMySQLDate($searchRule['_officialcources_headers.date_doc>=']); unset($searchRule['_officialcources_headers.date_doc>=']); } 
			if(isset($searchRule['_officialcources_headers.date_doc<='])) { $whereArr['_officialcources_headers.date_doc<='] = $this->toMySQLDate($searchRule['_officialcources_headers.date_doc<=']); unset($searchRule['_officialcources_headers.date_doc<=']); } 
			if(isset($searchRule['_officialcources_rows.cource_date>='])) { $whereArr['_officialcources_rows.cource_date>='] = $this->toMySQLDate($searchRule['_officialcources_rows.cource_date>=']); unset($searchRule['_officialcources_rows.cource_date>=']); } 
			if(isset($searchRule['_officialcources_rows.cource_date>='])) { $whereArr['_officialcources_rows.cource_date<='] = $this->toMySQLDate($searchRule['_officialcources_rows.cource_date<=']); unset($searchRule['_officialcources_rows.cource_date<=']); }
			//var_dump($searchRule);
			if($whereArr)  $this->db->where($whereArr);
			$this->db->like($searchRule);
		}
		if($sort = $this->ciObject->GetSessionData('sort')){
			$this->db->orderby($sort['c'], $sort['r']);
		}
		$query = $this->DBGet('_documents');
		if($query->num_rows()>0) return $this->ciObject->GetPagingDS($query->result());
		return array();
	}
	
	public function GetCurrenciesList(){
		$this->db->select('_currencies.rid, _currencies.code as currency_code');
		$this->db->from('_currencies');
		$this->db->orderby('currency_code');
		$query = $this->DBGet('_currencies');
		if($query->num_rows()>0) return $query->result();
		return False;
	}
	
	public function GetEditDS(){
		$this->db->select('_documents.rid, 
							DATE_FORMAT(_officialcources_headers.date_doc, \'%d.%m.%Y\') as date_doc, 
							_officialcources_rows._currencies_rid as _currencies_rid,
							DATE_FORMAT(_officialcources_rows.cource_date, \'%d.%m.%Y\') as cource_date,
							_officialcources_rows.cource_value as cource_value,
							_currencies.code as currency_code,
							DATE_FORMAT(_documents.modifyDT, \'%d.%m.%Y %H:%i\') as modifyDT, 
							_documents.descr as descr, _documents.archive
							');
		$this->db->from('_documents');
		$this->db->join('_officialcources_headers', '_officialcources_headers._documents_rid = _documents.rid');
		$this->db->join('_officialcources_rows', '_officialcources_rows._officialcources_headers_rid = _officialcources_headers.rid');
		$this->db->join('_currencies', '_officialcources_rows._currencies_rid = _currencies.rid');
		$this->db->where(array('_documents.rid'=>$this->ciObject->GetSessionData('activerecord')));
		$query = $this->DBGet('_documents');
		if($query->num_rows()>0) return $query->row();
		return False;
	}
	
	public function CreateRecord(){
		$insDocumentArray = array('_documents_types_rid'=>$this->typeDocRid, 
									'descr'=>$_POST['descr'],
									'modifyDT'=>$this->nowTime,
									'modifier_users_rid'=>$this->userRid);
		$this->db->trans_begin();
		$this->db->insert('_documents', $insDocumentArray);
		$docRid = $this->db->insert_id();
		$insHeaderArray = array('date_doc'=>$this->toMySQLDate($_POST['date_doc']),
							'_documents_rid'=>$docRid,
							'archive'=>$_POST['archive'],
							'modifier_users_rid'=>$this->userRid,
							'modifyDT'=>$this->nowTime);

		$this->db->insert('_officialcources_headers', $insHeaderArray);
		$insRowsArray = array('_officialcources_headers_rid'=>$this->db->insert_id(),
							'_currencies_rid'=>$_POST['_currencies_rid'],
							'cource_date'=>$this->toMySQLDate($_POST['cource_date']),
							'cource_value'=>$_POST['cource_value'],
							'archive'=>$_POST['archive'],
							'modifier_users_rid'=>$this->userRid,
							'modifyDT'=>$this->nowTime);
		$this->db->insert('_officialcources_rows', $insRowsArray);
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return $docRid;
		}		
	}
	
	public function UpdateRecord(){
		$updateDocumentArray = array('_documents_types_rid'=>$this->typeDocRid, 
									'descr'=>$_POST['descr'],
									'modifyDT'=>$this->nowTime,
									'modifier_users_rid'=>$this->userRid);
		$this->db->trans_begin();
		$this->db->update('_documents', $updateDocumentArray, array('_documents.rid'=>$this->ciObject->GetSessionData('activerecord')));
				
		$updateHeaderArray = array('date_doc'=>$this->toMySQLDate($_POST['date_doc']),
								'archive'=>$_POST['archive'],
								'modifier_users_rid'=>$this->userRid,
								'modifyDT'=>$this->nowTime);

		$this->db->update('_officialcources_headers', $updateHeaderArray, array('_officialcources_headers._documents_rid'=>$this->ciObject->GetSessionData('activerecord')));
		$query = $this->db->getwhere('_officialcources_headers', array('_officialcources_headers._documents_rid'=>$this->ciObject->GetSessionData('activerecord')));
		if(!$query->num_rows()) {
			$this->db->trans_rollback();
    		return False;
		}
		$row = $query->row();
		$updateRowsArray = array('_currencies_rid'=>$_POST['_currencies_rid'],
							'cource_date'=>$this->toMySQLDate($_POST['cource_date']),
							'cource_value'=>$_POST['cource_value'],
							'archive'=>$_POST['archive'],
							'modifier_users_rid'=>$this->userRid,
							'modifyDT'=>$this->nowTime);
		$this->db->update('_officialcources_rows', $updateRowsArray, array('_officialcources_rows._officialcources_headers_rid'=>$row->rid));
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return $this->ciObject->GetSessionData('activerecord');
		}		
	}
	
	public function DeleteRecord(){
		$this->db->trans_begin();
		$this->db->delete('_documents', array('rid'=>$this->ciObject->GetSessionData('activerecord')));
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return True;
		}		
	}
	
	public function GetCource($p_currencyRid, $p_baseCurrencyRid, $p_Date = null){
		if(!$p_Date) $p_Date = date('Y-m-d');
		$this->db->select('_officialcources_rows.*');
		$this->db->from('_officialcources_rows');
		$this->db->where(array('cource_date<='=>$p_date, '_currencies_rid'=>$p_currencyRid, '_base_currencies_rid'=>$p_baseCurrencyRid));
		$this->db->orderby('cource_date');
		$this->db->limit('1');
		$query = $this->db->get();
		if($query->num_rows()) return $query->row();
		return false;
	}
}
?>